package com.zds.passwordbox;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zds.passwordbox.adapter.PageFragmentAdapter;
import com.zds.passwordbox.common.Constant;
import com.zds.passwordbox.data.dao.AccountDao;
import com.zds.passwordbox.data.dao.DBHelper;
import com.zds.passwordbox.data.dao.PrivateKeyDao;
import com.zds.passwordbox.fragment.AccountFragment;
import com.zds.passwordbox.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 pager2;

    private final String[] menuTexts = {/*"验证",*/ "账号", "我的"};
    private final int[] menuIcons = {/*R.drawable.tab_menu_code, */R.drawable.tab_menu_account,
            R.drawable.tab_menu_mine};
    List<Fragment> fragmentList = new ArrayList<>();

    AccountFragment accountFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 初始化数据库
        DBHelper dbHelper = DBHelper.getInstance(MainActivity.this, Constant.DB_NAME);
        AccountDao accountDao = AccountDao.getInstance(dbHelper.open());
        PrivateKeyDao keyDao = PrivateKeyDao.getInstance(dbHelper.open());
        // 初始化Fragment
//        fragmentList.add(new CodeFragment(accountDao));

        accountFragment = new AccountFragment(accountDao);
        fragmentList.add(accountFragment);
        fragmentList.add(new MineFragment(keyDao, accountDao));
        // 获取ViewPager实例
        pager2 = findViewById(R.id.pager);

        // 设置自定义页面切换动画(暂未找到解决上下滑动也会触发左右滑动的方法，先禁用左右滑动)
        //pager2.setPageTransformer(new CustomPageTransformer());
        pager2.setUserInputEnabled(false);
        pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                // 表示Android的菜单内容已更改，应重新绘制菜单,
                // 此方法是操作系统调用 onPrepareOptionsMenu() 的信号
                invalidateOptionsMenu();
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

        // 初始化TabLayout
        initBottomTab();

    }

    private void initBottomTab() {
        // 将Fragment和ViewPager关联起来
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        PageFragmentAdapter pageFragmentAdapter = new PageFragmentAdapter(MainActivity.this,
                fragmentList);
        // 给ViewPager2设置适配器
        pager2.setAdapter(pageFragmentAdapter);
        new TabLayoutMediator(tabLayout, pager2, false, false,
                (tab, position) -> {
                    // 设置Tab的标题
                    tab.setText(menuTexts[position]);
                    // 设置Tab的图标和标题
                    tab.setCustomView(initTabItem(menuIcons[position], menuTexts[position]));
                }).attach();
    }

    private View initTabItem(int image, String title) {
        ImageView menuIcon;
        TextView menuText;
        View view = LayoutInflater.from(this).inflate(R.layout.tab_menu, null);
        menuIcon = view.findViewById(R.id.menuIcon);
        menuText = view.findViewById(R.id.menuText);
        menuText.setText(title);
        menuIcon.setImageResource(image);
        return view;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // 动态设置ToolBar状态
        switch (pager2.getCurrentItem()) {
            case 0:
            case 1:
                break;
            case 2:
                menu.clear();
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public void refreshAccountList() {
        if (accountFragment != null) {
            accountFragment.setImport(true);
        }
    }
}